/*Entry Cost Analysis
STATA 14
January 12, 2017
Firm-Level Balance Tests*/


cd  "C:\Users\ejm5\Dropbox\Rents_Asunder\CountryofOrigin\20160315_InternationaOrganization\Replication"
use  oecd_7years_final.dta, clear
log using balance.smcl, replace
set more off

/*Drop all firms that entered very early*/
drop if a1<2005


/*Sectors*/
#delimit;
generate constant=1;



/**Table A2*************************************************************************************************************/

#delimit;
tab sector_plus, gen(sect);


#delimit;
xi: reg labor_est phase3 oecd inter_p3oecd, cluster(country_id); 
outreg2 using balance, ci tdec(3) bdec(3) replace;

#delimit;
foreach x in labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e a15_f  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f c1_1 c2  e5_2013_1 e5_2013_2 a9 
{;
xi: reg `x' phase3 oecd inter_p3oecd i.year, cluster(country_id); 
outreg2 using balance, ci tdec(3) bdec(3); 
};

outreg2 using balance, tdec(3) bdec(3) excel;

#delimit;
xi: reg lurc phase3 oecd inter_p3oecd i.year, cluster(country_id); 
outreg2 using balance, ci tdec(3) bdec(3);
xi: reg a15_f phase3 oecd inter_p3oecd i.year, cluster(country_id); 
outreg2 using balance, ci tdec(3) bdec(3);
xi: reg a17_f phase3 oecd inter_p3oecd i.year, cluster(country_id); 
outreg2 using balance, ci tdec(3) bdec(3) excel;


/**Table A3*************************************************************************************************************/

#delimit;
preserve;
scalar drop _all;
set more off;
foreach x in c6 e9 labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f   c1_1 c2  e5_2013_1 e5_2013_2 a9 phase3 oecd inter_p3oecd year a1  p_polity chga_dem ln_gdp_pc ln_pop 
{;
ttest `x', by(treatment);
scalar define t_mean_`x'= r(mu_1);
scalar define c_mean_`x'= r(mu_2);
scalar define t_sd_`x'= r(sd_1);
scalar define c_sd_`x'= r(sd_2);
scalar define p_`x'=r(p);
scalar define t_`x'=r(t);
};

collapse pci_id;

#delimit;
set more off;
foreach x in c6 e9 labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f   c1_1 c2  e5_2013_1 e5_2013_2 a9 phase3 oecd inter_p3oecd year a1  p_polity chga_dem ln_gdp_pc ln_pop  
{;
generate t_mean_`x'= t_mean_`x';
generate c_mean_`x'= c_mean_`x';
generate t_sd_`x'= t_sd_`x';
generate c_sd_`x'= c_sd_`x';
generate p_`x'=p_`x';
generate t_`x'=t_`x';
};

#delimit;
drop pci_id;
generate id=_n;
reshape long t_mean_ t_sd_ c_mean_ c_sd_ p_ t_, i(id) j(var) string;
drop if t_mean==.;
replace id=_n;
save ttest_FDI.dta, replace;


#delimit;
use  oecd_7years_working.dta, clear;
drop if a1<2005;
keep c6 e9  labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f   c1_1 c2  e5_2013_1 e5_2013_2 a9 phase3 oecd inter_p3oecd year a1  p_polity chga_dem ln_gdp_pc ln_pop  ;

order c6 e9 labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f   c1_1 c2  e5_2013_1 e5_2013_2 a9 phase3 oecd inter_p3oecd year a1 p_polity chga_dem ln_gdp_pc ln_pop  ;

describe, replace clear;
drop type isnumeric format vallab;
rename name var;
sort var;

merge 1:1 var using ttest_FDI.dta;
sort position;
save ttest_FDI.dta, replace;
outsheet using "ttest_FDI", comma replace;
restore;




/************************************************Table A5********************************************/
#delimit;

generate bribe_dich=1 if e6>=8;
replace bribe_dich=0 if e6==8;


#delimit;
sum bribe_dich e6 c6 e9 phase3 oecd inter_p3oecd a1 enforce_scale_entry oecd_enforce_simple d2 labor_est hundred ln_gdp_pc p_polity ln_pop y*;


/**Table A1*************************************************************************************************************/

#delimit;
set more off;
foreach x in c6 e9 labor_est equity_est hundred d2 lurc a6_1 a6_2 a6_3 a6_4 a6_5 a6_6 restrict  a13 ln_sales a15_a a15_b a15_c a15_d a15_e  ln_expenditures 
a17_a a17_b a17_c a17_d a17_e a17_f   c1_1 c2  e5_2013_1 e5_2013_2 a9 phase3 oecd inter_p3oecd year a1  p_polity chga_dem ln_gdp_pc ln_pop 
{;
ttest `x', by(oecd);
};




/****************************Graphs Sector*********************************/

#delimit
preserve;
collapse (count) constant if a1>=2000, by(sector_plus phase3 oecd);
by phase3 oecd, sort: egen total_investors=sum(constant);
generate percentage= (constant/ total_investors)*100;
tabstat total_investors, by(phase3);

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==1, stack over(sect, sort(percentage) descending label(labcolor(black)labsize(tiny) valuelabel)) ytitle("")  blabel(bar, size(tiny) format(%9.1g))
title("Signatory, Pre-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off) scheme(s1mono);
graph save sector_preOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==1, stack over(sect, sort(percentage) descending label(labcolor(black)labsize(tiny) valuelabel)) ytitle("")  blabel(bar, size(tiny) format(%9.1g))
title("Signatory, Post-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off) scheme(s1mono);
graph save sector_postOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==0, stack over(sect, sort(percentage) descending label(labcolor(black)labsize(tiny) valuelabel)) ytitle("")  blabel(bar, size(tiny) format(%9.1g))
title("Non-Signatory, Pre-Phase 3 ", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off) scheme(s1mono);
graph save sector_prenonOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==0, stack over(sect, sort(percentage) descending label(labcolor(black)labsize(tiny) valuelabel)) ytitle("")  blabel(bar, size(tiny) format(%9.1g))
title("Non-Signatory, Post-Phase 3", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off) scheme(s1mono);
graph save sector_postnonOECD.gph, replace;

#delimit;
graph combine sector_preOECD.gph sector_postOECD.gph sector_prenonOECD.gph sector_postnonOECD.gph ,  xcommon ycommon  rows(2) cols(2)
title("Share of Firms in Each ISIC Revision 4 Sector (%)", position(6) size(medlarge) color(black)) scheme(s1mono);
graph save Sector.gph, replace;
graph export Sector.tif, as(tif) replace;
restore;



/*Labor Size*/

#delimit;
preserve;
collapse (count) constant if a1>=2000, by(labor_est phase3 oecd);
by phase3 oecd, sort: egen total_investors=sum(constant);
generate percentage= (constant/ total_investors)*100;
tabstat total_investors, by(phase3);

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==1, stack over(labor_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Signatory, Pre-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off);
graph save labor_preOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==1, stack over(labor_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Signatory, Post-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off);
graph save labor_postOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==0, stack over(labor_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Non-Signatory, Pre-Phase 3 ", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off);
graph save labor_prenonOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==0, stack over(labor_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Non-Signatory, Post-Phase 3", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off);
graph save labor_postnonOECD.gph, replace;


#delimit;
graph combine labor_preOECD.gph labor_postOECD.gph labor_prenonOECD.gph labor_postnonOECD.gph , xcommon ycommon  rows(2) cols(2)
title("Share of Firms in Each Labor Size Category", position(6) size(medlarge) color(black));
graph save Labor.gph, replace;
graph export Labor.tif, as(tif) replace;
restore;






/*Equity Size*/

#delimit;
preserve;
collapse (count) constant if a1>=2000, by(equity_est phase3 oecd);
by phase3 oecd, sort: egen total_investors=sum(constant);
generate percentage= (constant/ total_investors)*100;
tabstat total_investors, by(phase3);

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==1, stack over(equity_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Signatory, Pre-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off);
graph save equity_preOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==1, stack over(equity_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Signatory, Post-Phase 3", size(medlarge)) bar(1, fcolor(navy)) legend(off);
graph save equity_postOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==0 & oecd==0, stack over(equity_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Non-Signatory, Pre-Phase 3 ", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off);
graph save equity_prenonOECD.gph, replace;

#delimit;
graph hbar (sum) percentage if phase3==1 & oecd==0, stack over(equity_est,  label(labcolor(black)labsize(small) valuelabel))  blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Non-Signatory, Post-Phase 3", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off);
graph save equity_postnonOECD.gph, replace;


#delimit;
graph combine equity_preOECD.gph equity_postOECD.gph equity_prenonOECD.gph equity_postnonOECD.gph , xcommon ycommon  rows(2) cols(2)
title("Share of Firms in Each Equity Size Category", position(6) size(medlarge) color(black));
graph save equity.gph, replace;
graph export equity.tif, as(tif) replace;
restore;



#delimit
collapse (count) constant, by(country_id oecd);
by oecd, sort: egen total_investors=sum(constant);
generate percentage= (constant/ total_investors)*100;
drop if country_id==64;


#delimit;
graph hbar (sum) percentage if  oecd==0, stack over(country_id, sort(percentage) descending label(labcolor(black)labsize(small) valuelabel)) blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("Non-OECD", size(medlarge)) bar(1, lcolor(maroon) fcolor(maroon)) legend(off);
graph save countrynonOECD.gph, replace;


#delimit;
graph hbar (sum) percentage if  oecd==1, stack over(country_id, sort(percentage) descending label(labcolor(black)labsize(small) valuelabel)) blabel(bar) ytitle("") blabel(bar, size(vsmall) format(%9.1g))
title("OECD-ABC", size(medlarge)) bar(1, lcolor(navy) fcolor(navy)) legend(off);
graph save countryOECD.gph, replace;


#delimit;
graph combine countrynonOECD.gph countryOECD.gph ,  xcommon ycommon  rows(2) cols(2)
title("Share of Firms in Each ISIC Revision 4 Sector (%)", position(6) size(medlarge) color(black));
graph save counrty.gph, replace;
graph export country.tif, as(tif) replace;
log close;


